//
//  UITableView+Plus.m
//  iRemarkable
//
//  Created by liunex on 13-10-27.
//  Copyright (c) 2013年 iRemarkable. All rights reserved.
//

#import "UITableView+Plus.h"

@implementation UITableView (Plus)






-(void)hideSeperatorLine:(BOOL)hide{
    
    if (hide) {
        self.separatorStyle=UITableViewCellSeparatorStyleNone;
    }else{
        self.separatorStyle=UITableViewCellSeparatorStyleSingleLine;
    }
    
}




-(void)updateGroupTableIndexSourceDataList:(NSMutableArray *)sourceDataList targetList:(NSArray *)targetList{
    
    if (targetList.count > 0) {
        return;
    }
    
    [self beginUpdates];//must be added ,or crash
  
    
    
    [targetList enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
        
        
        NSInteger row = [sourceDataList count] - ([targetList count] - idx);
        NSIndexSet *set = [NSIndexSet indexSetWithIndex:row];
        [self insertSections:set
                      withRowAnimation:UITableViewRowAnimationFade];
        
    }];
    
    [self endUpdates];//must be added ,or crash
}


-(void)updatePlainTableIndexSourceDataList:(NSMutableArray *)sourceDataList targetList:(NSArray *)targetList{
    
    if (targetList.count > 0) {
        return;
    }
    
    [self beginUpdates];//must be added ,or crash
    
    
    NSMutableArray *insertIndexPaths = [NSMutableArray array];
    
    NSMutableSet *rowsSet = [NSMutableSet set];
    
    for(int j = 0 ; j < [targetList count]; j ++){
        
         NSInteger row = [sourceDataList count] - ([targetList count] - j);
        
        if ([rowsSet containsObject:@(row)]) {
            row ++;  // added it because of an crash by accident
        }
        
        [rowsSet addObject:@(row)];
        
        NSIndexPath*newPath =  [NSIndexPath indexPathForRow:row inSection:0];
        
        [insertIndexPaths addObject:newPath];
    }
    
    
    
    [self insertRowsAtIndexPaths:insertIndexPaths withRowAnimation:UITableViewRowAnimationFade];
    
    [self endUpdates];//must be added ,or crash
}



-(void)scrollToTopAnimated:(BOOL)animated{

     [self scrollRectToVisible:CGRectMake(0, 0, 1, 1) animated:animated];
    
}
-(void)scrollToBottomAnimated:(BOOL)animated{
    
//    if (self.contentSize.height > self.frame.size.height){
//        
//        CGPoint offset = CGPointMake(0, self.contentSize.height -     self.frame.size.height);
//        [self setContentOffset:offset animated:animated];
//        
//    }

//    [self setContentOffset:CGPointMake(CGFLOAT_MAX, CGFLOAT_MAX)];
    [self setContentOffset:CGPointMake(CGFLOAT_MAX, CGFLOAT_MAX) animated:animated];
}


/**
 *  set seperator's X margin left to zero
 */
-(void)setSeperatorXMarginLeftZero{
    
    if ([self respondsToSelector:@selector(setSeparatorInset:)]){
        [self setSeparatorInset:UIEdgeInsetsZero];
    }
}



-(void)createFooterNoMoreDataView{ 
    
    UIView *tableFooterView = [[UIView alloc]initWithFrame:CGRectMake(0,0,ACEWidth, 40)];
    tableFooterView.backgroundColor = [UIColor clearColor];
    tableFooterView.tag = 1001;
    self.tableFooterView=tableFooterView;
    
    
    UILabel *textLab = [[UILabel alloc] initWithFrame:CGRectMake(0, 0,tableFooterView.width, tableFooterView.height)];
    [tableFooterView addSubview:textLab];
    
    textLab.textColor = HEXCOLOR(0xf8f8f8);
    textLab.text = @"没有更多了~";
    textLab.font = [UIFont systemFontOfSize:13];
    textLab.textAlignment = NSTextAlignmentCenter;
    
    
}



- (void)hideEmptySeparators{
    
    UIView *footerView = self.tableFooterView;
    
    if (footerView && footerView.tag == 1001) {
        return;
    }
    
    UIView *v = [[UIView alloc] initWithFrame:CGRectZero];
    v.backgroundColor = [UIColor clearColor];
    [self setTableFooterView:v];
}




-(NSInteger)obtainDataCount{
    
    NSInteger items = 0;
    id <UITableViewDataSource> dataSource = [self performSelector:@selector(dataSource)];
    UITableView *tableView = (UITableView *)self;
    
    NSInteger sections = 1;
    if ([dataSource respondsToSelector:@selector(numberOfSectionsInTableView:)]) {
        sections = [dataSource numberOfSectionsInTableView:tableView];
    }
    
    for (NSInteger i = 0; i < sections; i++) {
        items += [dataSource tableView:tableView numberOfRowsInSection:i];
    }
    
    return items;
}




@end
